﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YunCMS.Common;
using YunCMS.BLL;
using YunCMS.Model;
using YunCMS.Controls.Page;

namespace YunCMS.Web.Admin.User
{
    public partial class UserLog : System.Web.UI.Page
    {
        private YCUserLogBLL _bll = null;

        protected YCUserLogBLL BLL
        {
            get
            {
                if (_bll == null)
                {
                    _bll = new YCUserLogBLL();
                }
                return _bll;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls(true);
            }
        }

        private void BindControls(bool isFirst)
        {
            string title = keywordsTB.Text.Trim();

            DateTime? begin = null,
                 end = null;
            if(!string.IsNullOrEmpty(beginDateTB.Text.Trim()))
            {
                begin = DateTime.Parse(beginDateTB.Text.Trim());
            }
            if (!string.IsNullOrEmpty(endDateTB.Text.Trim()))
            {
                end = DateTime.Parse(endDateTB.Text.Trim());
            }

            if (isFirst)
            {
                AspNetPager1.RecordCount = BLL.GetCount(title, begin, end);
            }

            FillRepeater(title, begin, end);
        }

        private void FillRepeater(string title, DateTime? begin, DateTime? end)
        {
            int pageSize = AspNetPager1.PageSize,
               pageCurrent = AspNetPager1.CurrentPageIndex;

            IList<YCUserLog> list = BLL.GetList(title, begin, end, pageSize, pageCurrent);

            DataListRP.DataSource = list;
            DataListRP.DataBind();

        }
        private IList<int> AllSelectedItems()
        {
            List<int> ids = new List<int>() { };
            for (int i = 0; i < DataListRP.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)DataListRP.Items[i].FindControl("idCHB");
                if (cb.Checked)
                {
                    int id = Convert.ToInt32(((Label)DataListRP.Items[i].FindControl("idLabel")).Text);
                    ids.Add(id);
                }
            }
            return ids;
        }

        protected void deleteAllBT_Click(object sender, EventArgs e)
        {
            IList<int> ids = AllSelectedItems();
            foreach (int id in ids)
            {
                BLL.Remove(id);
            }
            BindControls(true);
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindControls(false);
        }

        protected void searchBT_Click(object sender, EventArgs e)
        {
            BindControls(true);
        }
    }
}